package com.podhradie.net.themperature.android;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;

import com.podhradie.net.themperature.android.logic.Constants;
import com.podhradie.net.themperature.android.widget.ThemperatureWidgetProvider;

public class SettingsActivity extends PreferenceActivity {
	
	 EditText urlField;
	 EditText portFied;
	 Context appContext;

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        appContext = this.getApplicationContext();
	        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	        addPreferencesFromResource(R.xml.preferences);
	        
	        ListPreference myVar = (ListPreference) findPreference(Constants.WIDGET_UPDATE_TIME_PREFERENCE);
	        myVar.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

	            public boolean onPreferenceChange(Preference preference, Object newValue) {
	            	String selectedValue = (String) newValue;
	            	if (selectedValue.equals(Constants.NO_WIDGET_UPDATE)){
	            		stopAlarmService();
	            	}else {
	            		startAlarmService(Integer.parseInt(selectedValue));
	            	}
	            	return true;
	            }
	        });
	    }
	    
	    
		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			return super.onKeyDown(keyCode, event);
		}
		
		private void startAlarmService (int secondTime){
			Log.d("debug", "StartAlarmService, secondTime "+secondTime);
			
	        Intent intent = new Intent(this.getApplicationContext(), ThemperatureWidgetProvider.class);
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
			ComponentName componentName = new ComponentName(getApplicationContext(), ThemperatureWidgetProvider.class);
	        int[] ids = appWidgetManager.getAppWidgetIds(componentName);
	        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
	        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
	        intent.putExtra(Constants.FILL_READ_PARAM, true);
	        PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	        
	        // Get the AlarmManager service
	        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
	        am.cancel(sender);
	        am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), secondTime * 1000, sender);
			
		}
		
		private void stopAlarmService (){
			Log.d("debug", "StopAlarmService");
			
	        Intent intent = new Intent(this.getApplicationContext(), ThemperatureWidgetProvider.class);
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
			ComponentName componentName = new ComponentName(getApplicationContext(), ThemperatureWidgetProvider.class);
	        int[] ids = appWidgetManager.getAppWidgetIds(componentName);
	        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
	        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
	        intent.putExtra(Constants.FILL_READ_PARAM, true);
	        PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
	        
	        // Get the AlarmManager service
	        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
	        am.cancel(sender);
			
		}

}
